package com.www.applyword.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;
import com.www.applyword.R;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.ui.chat.AddMoreActivity;
import com.www.applyword.ui.chat.FabuDynaminActivity;
import com.www.applyword.ui.chat.MyDynaminActivity;
import com.www.applyword.ui.chat.StartGroupChatActivity;
import com.www.applyword.utils.Utils;
import com.www.applyword.view.CustomPopWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/10/18 0018 15:56
 * version: 1.0
 */
public class ChatFragment extends BaseFragment {
    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.ib_back)
    ImageButton ibBack;
    @BindView(R.id.head_title)
    TextView headTitle;
    @BindView(R.id.ib_right)
    ImageButton ib_right;
    @BindView(R.id.tv_right)
    TextView tv_right;

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> tabs = new ArrayList<>();
    private CustomPopWindow customPopWindow, customPopWindow2;

    @Override
    protected int getLayoutResource() {
        return R.layout.chat_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initView();
    }


    private void initData() {
        tabs.clear();
        fragments.clear();
        tabs.add("消息");
        tabs.add("通讯录");
        tabs.add("好友动态");
        tabs.add("地区动态");
        fragments.add(new MessageFragment());
        fragments.add(new BossFragment());
        fragments.add(new FriendsDynaminFragment());
        fragments.add(new AreaDynaminFragment());
    }


    private void initView() {
        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        viewPager.setAdapter(new TabAdapter(getChildFragmentManager()));
        //关联ViewPager和TabLayout
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(fragments.size());

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        headTitle.setText("消息");
                        tv_right.setVisibility(View.VISIBLE);
                        ib_right.setVisibility(View.GONE);
                        ibBack.setVisibility(View.GONE);
                        break;
                    case 1:
                        headTitle.setText("通讯录好友");
                        tv_right.setVisibility(View.VISIBLE);
                        ib_right.setVisibility(View.GONE);
                        ibBack.setVisibility(View.GONE);
                        break;
                    case 2:
                        headTitle.setText("好友动态");
                        ib_right.setVisibility(View.VISIBLE);
                        ibBack.setVisibility(View.VISIBLE);
                        tv_right.setVisibility(View.GONE);
                        break;
                    case 3:
                        headTitle.setText("地区动态");
                        ib_right.setVisibility(View.VISIBLE);
                        ibBack.setVisibility(View.VISIBLE);
                        tv_right.setVisibility(View.GONE);
                        break;
                    default:

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.ib_back, R.id.ib_right, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                Utils.startActivity(MyDynaminActivity.class);
                break;
            case R.id.ib_right:
                Utils.startActivity(FabuDynaminActivity.class);
                break;
            case R.id.tv_right:
                showAddPopWindow();
                break;
            default:
        }
    }

    private void showAddPopWindow() {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.popwindow_message_contact, null);
            int popWindowWidth = (int) (Utils.getDeviceDensity().widthPixels / 3);
            PopupWindow mPopWindow = new PopupWindow(view,popWindowWidth, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            mPopWindow.setContentView(view);
            // 设置PopupWindow的背景
            mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mPopWindow.setOutsideTouchable(true);
            mPopWindow.setTouchable(true);
            mPopWindow.getContentView().setFocusable(true); // 这个很重要
            mPopWindow.getContentView().setFocusableInTouchMode(true);

        LinearLayout ll_phone = view.findViewById(R.id.ll_phone);
        LinearLayout ll_weixin = view.findViewById(R.id.ll_weixin);

            mPopWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        mPopWindow.dismiss();
                        //  mapView.setVisibility(View.VISIBLE);
                        return true;
                    }
                    return false;
                }
            });
            mPopWindow.showAsDropDown(tv_right, 0, 0);

         //  int popWindowWidth = (int) (Utils.getDeviceDensity().widthPixels / 3);
          //  int popWindowHeight = 250;
          //  int popWindowHeight = (int) (Utils.getDeviceDensity().heightPixels / 8);
         //   View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popwindow_message_contact, null);

           ll_phone.setOnClickListener(v -> {
                Intent intent = new Intent(Utils.getContext(), AddMoreActivity.class);
              //  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(TUIKitConstants.GroupType.GROUP, false);
                getActivity().startActivity(intent);
               mPopWindow.dismiss();
            });

            ll_weixin.setOnClickListener(v -> {
                Intent intent = new Intent(Utils.getContext(), StartGroupChatActivity.class);
               // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(TUIKitConstants.GroupType.TYPE, TUIKitConstants.GroupType.PUBLIC);
                getActivity().startActivity(intent);
                mPopWindow.dismiss();
            });

           /* customPopWindow = new CustomPopWindow.PopupWindowBuilder(getActivity())
                    .setView(contentView)
                    .size(popWindowWidth, popWindowWidth)
                    .setFocusable(true)
                    .setOutsideTouchable(true)
                    .create();*/
        }
              // customPopWindow.showAsDropDown(tv_right, 0, 0);



        class TabAdapter extends FragmentPagerAdapter {
            public TabAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            //显示标签上的文字
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs.get(position);
            }
        }
    }

